package com.hospes.rmes.asynctasks;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.hospes.rmes.App;
import com.hospes.rmes.MainActivity;
import com.hospes.rmes.constants.ConstMain;

import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: Andrew Khloponin
 * Date: 17.06.11
 */
public class SplashScreenAsyncTask extends AsyncTask<Void, Void, Boolean> {

	TextView tv;
	Activity act;
	ProgressBar progress;

	public SplashScreenAsyncTask(TextView v, ProgressBar progress, Activity act) {
		this.tv = v;
		this.act = act;
		this.progress = progress;
	}

	@Override
	protected Boolean doInBackground(Void... cont) {
		act.runOnUiThread(new Runnable() {
			public void run() {
				tv.setText("Creating images folder");
			}
		});
		File sddirImagesWeather = null;
		if (App.fileManager.isMounted()) {
			sddirImagesWeather = new File(ConstMain.DATA_EXTERNAL_IMAGE_STORAGE_FOLDER);
		} else {
			sddirImagesWeather = new File(ConstMain.DATA_INTERNAL_IMAGE_STORAGE_FOLDER);
		}
		System.out.println(sddirImagesWeather.getAbsolutePath() + "image folder created.............................................." + sddirImagesWeather.mkdirs());
		act.runOnUiThread(new Runnable() {
			public void run() {
				tv.setText("next step");
			}
		});
		act.runOnUiThread(new Runnable() {
			public void run() {
				tv.setText("other next step");
			}
		});
		return true;
	}

	@Override
	protected void onPostExecute(Boolean response) {
		try {
			tv.setText("all ok !");
			progress.clearAnimation();
			act.finish();
			Intent intent = new Intent(act, MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			act.startActivity(intent);
			Log.i(ConstMain.LOG_TAG, "Will start " + MainActivity.class.getName().toString());
		} catch (Exception e) {
			Log.e("Error: ", " " + e.getMessage());
		}
		super.onPostExecute(response);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}
}
